package com.demo.hotel_book.service;

import com.demo.hotel_book.entity.Hotel;

import com.demo.hotel_book.entity.HotelExample;
import com.demo.hotel_book.mapper.HotelMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import java.util.ArrayList;
import java.util.List;

@Service
public class HotelServiceImpl implements HotelService{

    String param;
    @Autowired
    HotelMapper hotelMapper;

   private static List<Hotel> listHotel=null;

    public List<Hotel> getListHotel() {
        return listHotel;
    }

    public void setListHotel(List<Hotel> listHotel) {
        this.listHotel = listHotel;
    }

    //关键字搜索函数
    public boolean SearchKey(String param, Model model){
        System.out.println("param="+param);
        if (!hotelMapper.seclectBycity(param).isEmpty()) {
            // System.out.println("city");
            this.param="city";
            System.out.println(hotelMapper.seclectBycity(param));
            listHotel = hotelMapper.seclectBycity(param);
            model.addAttribute("hotels",hotelMapper.seclectBycity(param));
        }
        else if (!hotelMapper.seclectBybrand(param).isEmpty()){
            // System.out.println("brand");
            this.param="brand";
            System.out.println(hotelMapper.seclectBybrand(param));
            listHotel=hotelMapper.seclectBybrand(param);
            model.addAttribute("hotels",hotelMapper.seclectBybrand(param));
        }

        else if (!hotelMapper.seclectByname("%"+param+"%").isEmpty()){
            //System.out.println("name");
            this.param="name";
            System.out.println(hotelMapper.seclectByname("%"+param+"%"));
            listHotel=hotelMapper.seclectByname("%"+param+"%");
            model.addAttribute("hotels",hotelMapper.seclectByname("%"+param+"%"));
        }
        else
            return true;
        return false;
    }



    Hotel min;
    public List<Hotel> PriceSortAsc(){
        for (int i=0;i<listHotel.size();i++){
            for (int j=i+1;j<listHotel.size();j++){
                if (listHotel.get(i).getRatesFrom()>listHotel.get(j).getRatesFrom())
                {
                    min=listHotel.get(j);
                    listHotel.set(j,listHotel.get(i));
                    listHotel.set(i,min);
                }
            }
        }
        return listHotel;
    }


     public List<Hotel> HeatSortAsc(){
         for (int i=0;i<listHotel.size();i++){
             for (int j=i+1;j<listHotel.size();j++){
                 if (listHotel.get(i).getNumberOfReviews()>listHotel.get(j).getNumberOfReviews())
                 {
                     min=listHotel.get(j);
                     listHotel.set(j,listHotel.get(i));
                     listHotel.set(i,min);
                 }
             }
         }
         return listHotel;
    }

    Hotel Max;
    public List<Hotel> PriceSortDes(){
        for (int i=0;i<listHotel.size();i++){
            for (int j=i+1;j<listHotel.size();j++){
                if (listHotel.get(i).getRatesFrom()<listHotel.get(j).getRatesFrom())
                {
                    Max=listHotel.get(j);
                    listHotel.set(j,listHotel.get(i));
                    listHotel.set(i,Max);
                }
            }
        }
        return listHotel;

    }


    public List<Hotel> HeatSortDes(){
        for (int i=0;i<listHotel.size();i++){
            for (int j=i+1;j<listHotel.size();j++){
                if (listHotel.get(i).getNumberOfReviews()<listHotel.get(j).getNumberOfReviews())
                {
                    Max=listHotel.get(j);
                    listHotel.set(j,listHotel.get(i));
                    listHotel.set(i,Max);
                }
            }
        }
        return listHotel;

    }

    public List<Hotel> screen(String start,String end,List<Boolean> stars,String param){
        List<String> Stars=new ArrayList<>();
        System.out.println(start+end+stars);
        HotelExample hotelExample=new HotelExample();
            for (int i=0;i<6;i++)
            {
              if (stars.get(i))
              {
                  System.out.println(i);
                  Stars.add(""+i+"");
              }
            }
            if (Stars.isEmpty())
            {
                if (this.param.equals("city"))
                    hotelExample.createCriteria().andRatesFromBetween(start,end).andCityLike(param);
                else if (this.param.equals("brand"))
                    hotelExample.createCriteria().andRatesFromBetween(start,end).andBrandNameLike(param);
                else if (this.param.equals("name"))
                    hotelExample.createCriteria().andRatesFromBetween(start,end).andHotelTranslatedNameLike("%"+param+"%");
            }

            else
            {
                if (this.param.equals("city"))
                    hotelExample.createCriteria().andRatesFromBetween(start,end).andCityLike(param).andStarRatingIn(Stars);
                else if (this.param.equals("brand"))
                    hotelExample.createCriteria().andRatesFromBetween(start,end).andBrandNameLike(param).andStarRatingIn(Stars);
                else if (this.param.equals("name"))
                    hotelExample.createCriteria().andRatesFromBetween(start,end).andHotelTranslatedNameLike("%"+param+"%").andStarRatingIn(Stars);
            }

        return hotelMapper.selectByExample(hotelExample);
    }



}
